package com.watson.websocket.config1;

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

/**
 * //注解开启STOMP协议来传输基于代理的消息，此时控制器支持使用@MessageMapping
 */
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConf implements WebSocketMessageBrokerConfigurer {
//    @Autowired
//    private H5HeaderInterceptor h5HeaderInterceptor;
    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry){
        // websocket的连接路径
        registry.addEndpoint("/websocket").withSockJS();;

        // 可注册多个
        registry.addEndpoint("/queueServer")
                // 设置跨域
                .setAllowedOrigins("*")
                // 设置拦截器
//                .addInterceptors(HeaderInterceptor)
                // 支持 SockJS
                .withSockJS();
    }

    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry){
        //设置topic通信。设置接收客户端订阅的路径前缀（必须设置）
        registry.enableSimpleBroker("/topic", "/queue");
        //设置接收客户端消息的路径前缀
        registry.setApplicationDestinationPrefixes("/app");

        registry.setUserDestinationPrefix("/user");
    }

}
